<template>
    <div :class="className" :id="id" :style="{height:height,width:width}"></div>
</template>

<script>
    import echarts from 'echarts'


    export default {
        props: {
            id: {
                type: String,
                default: 'chart'
            },
            className: {
                type: String,
                default: 'chart'
            },
            width: {
                type: String,
                default: '100%'
            },
            height: {
                type: String,
                default: '300px'
            },
            con:{
                type: Object,
                default: ()=> {
                    return {}
                }
            }
        },
        data() {
            return {
                chart: null
            }
        },
        mounted() {
            var that=this
            window.addEventListener('resize', that.resize)
        },
        beforeDestroy() {
            var that=this
            if (!this.chart) {
                return
            }
            this.chart.dispose()
            this.chart = null
            window.removeEventListener('resize', that.resize)
        },
        methods: {
            resize(){
                this.chart.resize()
            },
            initChart() {
                let that=this
                this.chart = echarts.init(document.getElementById(this.id))
                this.chart.setOption({
                    backgroundColor: '#ffffff',
                    title: {
                        text: '会员分布图',
                        x: '20',
                        top: '20',
                        textStyle: {
                            color: '#ff721c',
                            fontSize: '22'
                        },
                        subtextStyle: {
                            color: '#90979c',
                            fontSize: '16'
                        }
                    },
                    tooltip: {
                        trigger: 'item',
                        formatter: '{a} <br/>{b} : {c} ({d}%)'
                    },
                    legend: {
                        left: 'center',
                        bottom: '20',
                        data: ['金牌推手总数', '会员总数']
                    },
                    calculable: true,
                    series: [
                        {
                            name: '当前',
                            type: 'pie',
                            roseType: 'radius',
                            radius: [15, 95],
                            center: ['50%', '47%'],
                            data: [
                                { value: that.con.jin, name: '金牌推手总数' },
                                { value: that.con.memberAll, name: '会员总数' },
                            ],
                            animationEasing: 'cubicInOut',
                            animationDuration: 2600
                        }
                    ]
                })
            }
        }
    }
</script>
